package com.ruoyi.wx.controller;

import java.util.Arrays;
import java.util.List;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.ruoyi.wx.domain.TGarbagesort;
import com.ruoyi.wx.service.ITGarbagesortService;


/**
 * 垃圾分类Controller
 *
 * @author ruoyi
 * @date 2020-05-01
 */
@Controller
@RequestMapping("/wx/garbagesort")
public class TGarbagesortController extends BaseController {
    private String prefix = "wx/garbagesort";

    @Autowired
    private ITGarbagesortService tGarbagesortService;

    @RequiresPermissions("wx:garbagesort:view")
    @GetMapping()
    public String garbagesort() {
        return prefix + "/garbagesort";
    }

    /**
     * 查询垃圾分类列表
     */
    @RequiresPermissions("wx:garbagesort:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TGarbagesort tGarbagesort) {
        startPage();
        return getDataTable(tGarbagesortService.list(new QueryWrapper<>(tGarbagesort)));
    }

    /**
     * 导出垃圾分类列表
     */
    @RequiresPermissions("wx:garbagesort:export")
    @Log(title = "垃圾分类", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TGarbagesort tGarbagesort) {
        List<TGarbagesort> list = tGarbagesortService.list(new QueryWrapper<>(tGarbagesort));
        ExcelUtil<TGarbagesort> util = new ExcelUtil<>(TGarbagesort.class);
        return util.exportExcel(list, "garbagesort");
    }

    /**
     * 新增垃圾分类
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存垃圾分类
     */
    @RequiresPermissions("wx:garbagesort:add")
    @Log(title = "垃圾分类", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TGarbagesort tGarbagesort) {
        return toAjax(tGarbagesortService.save(tGarbagesort));
    }

    /**
     * 修改垃圾分类
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        TGarbagesort tGarbagesort = tGarbagesortService.getById(id);
        mmap.put("tGarbagesort", tGarbagesort);
        return prefix + "/edit";
    }

    /**
     * 修改保存垃圾分类
     */
    @RequiresPermissions("wx:garbagesort:edit")
    @Log(title = "垃圾分类", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TGarbagesort tGarbagesort) {
        return toAjax(tGarbagesortService.updateById(tGarbagesort));
    }

    /**
     * 删除垃圾分类
     */
    @RequiresPermissions("wx:garbagesort:remove")
    @Log(title = "垃圾分类", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tGarbagesortService.removeByIds(Arrays.asList(Convert.toStrArray(ids))));
    }
}
